import { getUserInfo, getUserCollectOrCommitData, removeCollectionOrComment, addPosts, getMyPosts, DelMySendPosts, updateUserInfo } from '@/http/requestFunction'

export default {
    namespaced: true,
    actions: {
        // 获取用户信息
        async getUserInfo(content, username) {
            try {
                const res = await getUserInfo(username)
                content.commit('GETUSERINFO', res.data.info)
            } catch (e) {
                console.log(e)
            }
        },

        // 获取收藏列表
        async getUserCollentList(content, type) {
            try {
                const res = await getUserCollectOrCommitData(type)
                content.commit('GETCOLLENTLIST', res.data.myCollection)
            } catch (e) {
                console.log(e)
            }
        },

        // 删除收藏列表
        async removeCollentList(content, val) {
            try {
                const res = await removeCollectionOrComment(val.type, val.item)
            } catch (e) {
                console.log(e)
            }
        },

        // 获取点赞列表
        async getUserCommentList(content, type) {
            try {
                const res = await getUserCollectOrCommitData(type)
                // console.log(res)
                content.commit('GETUSERCOMMENTLIST', res.data.myCollection)
            } catch (e) {
                console.log(e)
            }
        },

        // 发布帖子
        async addPosts(content, info) {
            try {
                if (info.title === '' || info.content === '' || info.name === '') {
                    content.commit('TIPINFO', '添加信息不可为空！')
                }
                else {
                    const res = await addPosts(info.name, info.content, info.title)
                    content.commit('TIPINFO', '添加成功!')
                }

            } catch (e) {
                console.log(e)
            }
        },

        // 获取发布的信息
        async getMyPosts(content) {
            try {
                const res = await getMyPosts()
                content.commit('GETMYPOSTS', res.data.myPosts)
            } catch (e) {
                console.log(e)
            }
        },

        // 删除发布的信息
        async DelMySendPosts(content, item) {
            try {
                const res = await DelMySendPosts(item)
                console.log(res)
            }
            catch (e) {
                console.log(e)
            }
        },

        // 修改用户的个人信息
        async updateUserInfo(content, info) {
            try {
                const res = await updateUserInfo(info.username, info.sex, info.age, info.emial)
                const user = sessionStorage.getItem('user')
            } catch (e) {
                console.log(e)
            }
        }
    },

    mutations: {
        // 保存用户信息
        GETUSERINFO(state, info) {
            state.userInfo = info
        },
        // 保存用户收藏列表
        GETCOLLENTLIST(state, list) {
            state.collentList = list
        },

        // 保存用户点赞列表
        GETUSERCOMMENTLIST(state, list) {
            state.commentList = list
        },

        // 保存提示框信息
        TIPINFO(state, info) {
            state.tipShow = true
            state.tipText = info
            setTimeout(() => {
                state.tipText = ''
                state.tipShow = false
            }, 1000)
        },

        // 保存用户的发贴信息
        GETMYPOSTS(state, list) {
            state.myPosts = list
        }

    },

    state: {
        userInfo: {},
        collentList: [],
        commentList: [],
        tipShow: false,
        tipText: '',
        myPosts: []
    }

}